package com.yc.fav.servlet;

import com.yc.fav.entity.User;
import com.yc.fav.service.BizException;
import com.yc.fav.service.impl.UserServiceImpl;
import com.yc.fav.util.Utils;
import com.yc.fav.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/reg")
public class RegUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserServiceImpl userService = new UserServiceImpl();
        User user = new User();
        user.setName(req.getParameter("name"));
        user.setEmail(req.getParameter("email"));
        user.setPwd(req.getParameter("pwd"));
        user.setHead(req.getParameter("head"));
        Result result = null;
        try {
            userService.reg(user);
            result=new Result(1,"注册成功",null);
        } catch (BizException e) {
            result=new Result(e.getCode(),e.getMessage(),null);
        }
        Utils.output(resp,result);
    }
}
